package com.atguigu.school.controller;

import com.atguigu.school.commons.Page;
import com.atguigu.school.commons.Result;
import com.atguigu.school.pojo.Student;
import com.atguigu.school.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sms/studentController")
@Api(value = "学生控制器")
public class StudentController {

    /**
     * 处理学生相关业务
     */
    @Autowired
    private StudentService studentService;

    @PostMapping("/addOrUpdateStudent")
    @ApiOperation(value = "新增或更新学生信息", notes = "如果没有传入 id 就是新增，否则就是更新")
    public Result addOrUpdateStudent(@RequestBody Student student) {
        this.studentService.addOrUpdateStudent(student);
        return Result.ok();
    }

    @DeleteMapping("/delStudentById")
    @ApiOperation(value = "批量删除学生")
    @ApiImplicitParam(name = "ids", value = "预删除学生的 id 列表")
    public Result delStudentById(@RequestBody List<Integer> ids) {
        this.studentService.deleteStudentsByIds(ids);
        return Result.ok();
    }

    @GetMapping("/getStudentByOpr/{pageNo}/{pageSize}")
    @ApiOperation(value = "分页查询学生", notes = "目前只能根据名称和所属班级的名称查")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "pageNo", value = "当前页，从 1 开始"),
            @ApiImplicitParam(name = "pageSize", value = "每页长度")
    })
    public Result getStudentByOper(@PathVariable("pageNo") Integer pageNo, @PathVariable("pageSize") Integer pageSize, Student student) {
        Page<Student> page = this.studentService.selectStudentsByNameAndClazzName(student.getName(), student.getClazzName(), pageNo, pageSize);
        return Result.ok(page);
    }

}
